package com.mobilesalon;

import android.app.backup.BackupManager;
import android.content.Context;
import android.preference.PreferenceManager;

import com.mobilesalon.business.PreferencesManager;
import com.mobilesalon.business.ServiceClientManager;
import com.mobilesalon.business.ServiceClientReportManager;
import com.mobilesalon.calendar.CalendarProvider;
import com.mobilesalon.calendar.CalendarProviderGoogle;
import com.mobilesalon.contact.ContactProvider;
import com.mobilesalon.provider.DBHelper;
import com.mobilesalon.report.engine.ReportExporter;

public class MobileSalonFactory {

	//
	// Context
	//
	public static Context context;

	public synchronized static Context getContext() {
		if (context == null) {
			context = MobileSalonApplication.getAppContext();
			if (context == null) {
				throw new IllegalStateException("cannot retriece application context");
			}
		}
		return context;

	}

	//
	// DBHelper
	//
	private static DBHelper dbHelper;

	public synchronized static DBHelper getDBHelper() {
		if (dbHelper == null) {
			dbHelper = new DBHelper(getContext());
		}
		return dbHelper;
	}

	//
	// CalendarProvider
	//
	private static CalendarProvider calendarProvider;

	public static CalendarProvider getCalendarProvider() {
		if (calendarProvider == null) {
			calendarProvider = new CalendarProviderGoogle(getContext());
		}
		return calendarProvider;
	}

	//
	// ContactProvider
	//
	private static ContactProvider contactProvider;

	public static ContactProvider getContactProvider() {
		if (contactProvider == null) {
			contactProvider = new ContactProvider(getContext());
		}
		return contactProvider;
	}

	//
	// ServiceClientManager
	//
	private static ServiceClientManager serviceClientManager;

	public static synchronized ServiceClientManager getServiceClientManager() {
		if (serviceClientManager == null) {
			serviceClientManager = new ServiceClientManager(getDBHelper(), getCalendarProvider(),
					getContactProvider(), getPreferencesManager(), getBackupManager());
		}
		return serviceClientManager;
	}

	//
	// ReportExporter
	//
	private static ReportExporter reportExporter;

	public static ReportExporter getReportExporter() {
		if (reportExporter == null) {
			reportExporter = new ReportExporter();
		}
		return reportExporter;
	}
	
	//
	// ServiceClientReportManager
	//
	private static ServiceClientReportManager serviceClientReportManager;

	public static synchronized ServiceClientReportManager getServiceClientReportManager() {
		if (serviceClientReportManager == null) {
			serviceClientReportManager = new ServiceClientReportManager(getDBHelper(),
					getServiceClientManager(),getPreferencesManager(), getReportExporter());
		}
		return serviceClientReportManager;
	}

	//
	// SharedPreff
	//
	private static PreferencesManager preferencesManager;

	public static PreferencesManager getPreferencesManager() {
		if (preferencesManager == null) {
			preferencesManager = new PreferencesManager(
					PreferenceManager.getDefaultSharedPreferences(MobileSalonFactory.getContext()));
		}
		return preferencesManager;
	}
	
	//
	// BackupManager
	//
	private static BackupManager backupManager;

	public static BackupManager getBackupManager() {
		if (backupManager == null) {
			backupManager = new BackupManager(MobileSalonFactory.getContext());
		}
		return backupManager;
	}
	
}
